package com.sky.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.constant.StatusConstant;
import com.sky.dto.SetmealDTO;
import com.sky.dto.SetmealPageQueryDTO;
import com.sky.entity.Dish;
import com.sky.entity.Setmeal;
import com.sky.entity.SetmealDish;
import com.sky.exception.BaseException;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetMealDishMapper;
import com.sky.mapper.SetMealMapper;
import com.sky.result.PageResult;
import com.sky.service.SetMealService;
import com.sky.vo.DishItemVO;
import com.sky.vo.SetmealVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

@Service
@Slf4j
public class SetMealServiceImpl implements SetMealService {
    @Autowired
    private SetMealMapper setMealMapper;
    @Autowired
    private SetMealDishMapper setMealDishMapper;
    @Autowired
    private DishMapper dishMapper;

    @Override
    public PageResult selectSetMealList(SetmealPageQueryDTO setmealPageQueryDTO) {
        PageHelper.startPage(setmealPageQueryDTO.getPage(), setmealPageQueryDTO.getPageSize());
        Page<Setmeal> setmealPage = setMealMapper.selectList(setmealPageQueryDTO);

        return new PageResult(setmealPage.getTotal(), setmealPage.getResult());
    }

    @Override
    @Transactional
    public void addSetMeal(SetmealDTO setmealDTO) {
        // 新增基础信息
        Setmeal setmeal = new Setmeal();
        BeanUtils.copyProperties(setmealDTO, setmeal);

        log.info("setMeal: {}", setmeal);

        setMealMapper.insert(setmeal);

        // 拿到id
        Long id = setmeal.getId();
        // 拿到关联套餐菜品
        List<SetmealDish> setmealDishes = setmealDTO.getSetmealDishes();
        for (SetmealDish setmealDish : setmealDishes) {
            setmealDish.setSetmealId(id);
        }
        // 加入关联菜单
        setMealDishMapper.insert(setmealDishes);

    }

    @Override
    @Transactional
    public void changeStatus(Integer status, Long setMealId) {
        Setmeal setmeal = Setmeal.builder().status(status).id(setMealId).build();

        if(status == StatusConstant.ENABLE) {
            List<Long> dishIdList = setMealDishMapper.getDishIdBySetMealId(setMealId);
            List<Integer> statusList = dishMapper.getStatus(dishIdList);

            if(statusList.indexOf(StatusConstant.DISABLE) != -1) {
                throw new BaseException("菜品处于禁售，无法启售套餐！！！");
            }
        }


        setMealMapper.update(setmeal);
    }

    @Override
    @Transactional
    public SetmealVO getSetMealById(Long setMealId) {
        SetmealVO setmealVO = setMealMapper.selectById(setMealId);

        List<SetmealDish> setmealDishList = setMealDishMapper.selectBySetMealId(setMealId);

        setmealVO.setSetmealDishes(setmealDishList);

        return setmealVO;
    }

    @Override
    @Transactional
    public void updateSetMealInfo(SetmealDTO setmealDTO) {
        Setmeal setmeal = new Setmeal();
        // 修改基础信息
        BeanUtils.copyProperties(setmealDTO, setmeal);

        setMealMapper.update(setmeal);

        // 删除关联菜品
        List<Long> setMealIdList = new ArrayList<>();
        setMealIdList.add(setmeal.getId());
        setMealDishMapper.deleteBySetMealId(setMealIdList);

        // 重新加入
        List<SetmealDish> setmealDishes = setmealDTO.getSetmealDishes();
        for (SetmealDish setmealDish : setmealDishes ) {
            setmealDish.setSetmealId(setmeal.getId());
        }
        setMealDishMapper.insert(setmealDishes);
    }

    @Override
    @Transactional
    public void deleteSetMeal(List<Long> setMealIdList) {
        // 删除基本信息
        setMealMapper.delete(setMealIdList);

        // 删除关联菜品
        setMealDishMapper.deleteBySetMealId(setMealIdList);
    }

    @Override
    public List<Dish> selectSetMealByCategoryId(Long categoryId) {
        List<Dish> dishList = setMealMapper.selectByCategoryId(categoryId);

        return dishList;
    }

    @Override
    public List<DishItemVO> getSetMealDishBySetMealId(Long setMealId) {
        List<DishItemVO> dishItemVOList = setMealDishMapper.selectDishBySetMealId(setMealId);

        return dishItemVOList;
    }
}
